package com.info3k.server.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.info3k.client.domain.User;
import com.info3k.client.enums.PermissionTypes;
import com.info3k.client.enums.TabTypes;
import com.info3k.client.enums.UserTypes;
import com.info3k.client.utils.ToolsClient;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UserP implements Serializable {

	private static final long serialVersionUID = -1873799364997737422L;

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    
    @Persistent
	private String username;
    
    @Persistent
    private String password;
    
    @Persistent
    private String name;
    
    @Persistent
    private String surname;
    
    @Persistent
    private String email;
    
    @Persistent
    private String phone;
    
    @Persistent
    private UserTypes userType;
    
    @Persistent
    private Boolean activated;
    
    @Persistent
    private List<TabTypes> tabs;
    
    @Persistent
    private List<PermissionTypes> permissions;
    
    public UserP() {
    	
    }
    
    public UserP(User user) {
    	this.email = user.getEmail();
    	this.name = user.getName();
    	this.password = user.getPassword();
    	this.phone = user.getPhone();
    	this.surname = user.getSurname();
    	this.username = user.getUsername();
    	this.userType = user.getUserType();
    	this.activated = user.getActivated();
    	
    	this.tabs = new ArrayList<TabTypes>();
    	if(user.getTabs()!=null){
    		for (int i = 0; i < user.getTabs().length; i++) {
				this.tabs.add(user.getTabs()[i]);
			}
    	}
    	
    	this.permissions = new ArrayList<PermissionTypes>();
    	if(user.getPermissions()!=null){
    		for (int i = 0; i < user.getPermissions().length; i++) {
				this.permissions.add(user.getPermissions()[i]);
			}
    	}
    	
    	if(user.getId()!=null)
    		this.setId(KeyFactory.createKey(UserP.class.getSimpleName(), user.getId()));
    	else
    		this.setId(KeyFactory.createKey(UserP.class.getSimpleName(), System.currentTimeMillis()));
    }
    
    public static User createUser(UserP userP){
		User user = new User();
		user.setId(userP.getId().getId());
		user.setEmail(userP.getEmail());
		user.setName(userP.getName());
		user.setPassword(userP.getPassword());
		user.setPhone(userP.getPhone());
		user.setSurname(userP.getSurname());
		user.setUsername(userP.getUsername());
		user.setUserType(userP.getUserType());
		user.setActivated(userP.getActivated());
		
		if(userP.getTabs()!=null)
			user.setTabs(ToolsClient.fromCollectionToArray(userP.getTabs(), new TabTypes[userP.getTabs().size()]));
		
		if(userP.getPermissions()!=null)
			user.setPermissions(ToolsClient.fromCollectionToArray(userP.getPermissions(), new PermissionTypes[userP.getPermissions().size()]));
				
		return user;
    }
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public UserP(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}

	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getSurname() {
		return surname;
	}
	
	public void setSurname(String surname) {
		this.surname = surname;
	}
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getPhone() {
		return phone;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setId(Key id) {
		this.id = id;
	}

	public Key getId() {
		return id;
	}

	public void setUserType(UserTypes userType) {
		this.userType = userType;
	}

	public UserTypes getUserType() {
		return userType;
	}

	public void setTabs(List<TabTypes> tabs) {
		this.tabs = tabs;
	}

	public List<TabTypes> getTabs() {
		return tabs;
	}

	public void setPermissions(List<PermissionTypes> permissions) {
		this.permissions = permissions;
	}

	public List<PermissionTypes> getPermissions() {
		return permissions;
	}

	public void setActivated(Boolean activated) {
		this.activated = activated;
	}

	public Boolean getActivated() {
		return activated;
	}
}
